#include <iostream>
#include <iomanip>
#include <map>
//#include "sdk/DirectoryMonitor.h"
#include "sdk/way/FileSpaceWatcher.h"


void printStats(const FileSpaceWatcher& watcher) {
    auto stats = watcher.getFileStats();

    // 按文件大小排序

    // 打印前10大文件
    std::multimap<uint64_t, std::string, std::greater<uint64_t>> sortedFiles;
    for (const auto& [path, info] : stats) {
        sortedFiles.emplace(info.size,  path);
    }
    std::cout << "\nTop 10 largest files:\n";
    int count = 0;
    for (const auto& [size, path] : sortedFiles) {
        if (++count > 10) break;
        std::cout << std::setw(8) << size / 1024 << " KB\t" << path << "\n";
    }

    // 打印总文件数和总大小
    uint64_t totalSize = 0;
    for (const auto& [path, info] : stats) {
        totalSize += info.size;
    }

    std::cout << "\nTotal files: " << stats.size()
              << "\nTotal size: " << totalSize / (1024 * 1024) << " MB\n";
}

void updateCallback(const std::string& path, uint64_t size) {
    std::cout << "File updated: " << path << " (" << size << " bytes)\n";
}

int main(int argc, char* argv[]) {
//    if (argc < 2) {
//        std::cerr << "Usage: " << argv[0] << " <directory> [directory2 ...]\n";
//        return 1;
//    }

    FileSpaceWatcher watcher;
    watcher.setUpdateCallback(updateCallback);
    watcher.setLargeFileThreshold(50  * 1024 * 1024); // 50MB以上为大文件
    watcher.setFrequentUpdateThreshold(500);  // 500ms内多次更新视为频繁

//    for (int i = 1; i < argc; ++i){
        if (!watcher.addWatch("D:\\tool\\Notepad++" ,true)) {
//            std::cerr << "Failed to watch directory: " << argv[i] << "\n";
        }
//    }

    watcher.start();

    // 初始统计
    printStats(watcher);

    // 主循环
    std::cout << "\nMonitoring file changes. Press Enter to exit...\n";
    std::cin.get();

    watcher.stop();

    // 最终统计
    printStats(watcher);

    return 0;
}